const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('./pathConfig');
const webpack = require('webpack');

module.exports = (env) => {
   const config = {
      entry: {
         main: path.ENTRY,
         icon: path.ICON_ENTRY
       },
      resolve: {
         extensions: ['.tsx', '.ts', '.js'],
         alias: {
            '@': path.SRC,
         },
      },
      module: {
         rules: [
            {
               test: /\.(ts|js)x?$/,
               exclude: /node_modules/,
               use: [
                  {
                     loader: 'babel-loader',
                  },
               ],
            },
            {
               test: /\.css$/i,
               include: path.SRC,
               use: ['style-loader', 'css-loader', 'postcss-loader'],
            },
            {
               test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
               type: 'asset/resource',
            },
            {
               test: /\.(woff(2)?|eot|ttf|otf|svg)$/,
               type: 'asset/inline',
            },
         ],
      },
      plugins: [
         new HtmlWebpackPlugin({
            template: path.TEMPLATE,
            filename: 'index.html', // 输出文件名
            chunks: ['main'], // 包含的chunks
         }),
         new HtmlWebpackPlugin({
            template: path.TEMPLATE,
            filename: 'icon.html', // 输出文件名
            chunks: ['icon'], // 包含的chunks
         }),
         new webpack.DefinePlugin({
            'process.env': {
               CLIENT_ENV: JSON.stringify(env),
            },
         }),
      ],
   };

   return config;
};
